﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Data;

namespace VectorRacer
{
    public class Board2 : Canvas
    {
        public static readonly DependencyProperty CheckSizeProperty = DependencyProperty.Register("CheckSize", typeof(int), typeof(Board2));

        protected Dictionary<Player, PathSegmentCollection> _playersPath = new Dictionary<Player, PathSegmentCollection>();
        protected int _hChecks = 40;
        protected int _vChecks = 40;
        private Size _size;

        private LineSegment _newLineSegment = new LineSegment();

        public Board2()
        {
            _size = new Size(40 * 10, 40 * 10);
            this.Background = Brushes.Transparent;

            this.DataContextChanged += new DependencyPropertyChangedEventHandler(Board2_DataContextChanged);
            this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(Board2_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(Board2_MouseLeftButtonUp);
            this.MouseMove += new System.Windows.Input.MouseEventHandler(Board2_MouseMove);
        }

        void Board2_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        void Board2_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var checkXY = GetCheckXY(e.GetPosition(this));
            var player = GameSession.ActivePlayer;
            /*if (GameSession.CheckMove(checkXY))
                _newLineSegment.Point = new Point(checkXY.X * CheckSize, checkXY.Y * CheckSize);
            else
                _newLineSegment.Point = new Point(player.Position.X * CheckSize, player.Position.Y * CheckSize);*/
        }

        bool _isMouseClick = false;
        void Board2_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _isMouseClick = true;
        }

        void Board2_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_isMouseClick)
            {
                var check = GetCheckXY(e.GetPosition(this));
                var checkResult = GameSession.CheckMove(check);
                if (checkResult == ChechMoveResult.Correct)
                    GameSession.ActivePlayer.Move(check.X, check.Y);
                else if(checkResult == ChechMoveResult.Crash)
                {
                    var point = GameSession.GetCollisionPoint(check);
                    var c = new Ellipse() { Stroke = Brushes.Red, StrokeThickness = 1, Height = 5 };
                    Canvas.SetLeft(c, point.X);
                    Canvas.SetTop(c, point.Y);
                    this.Children.Add(c);
                }

                _isMouseClick = false;
            }
        }

        public void AddPlayer(Player player)
        {
            var playerLayer = new PlayerLayer(player, GameSession);
            var widthBinding = new Binding("ActualWidth");
            widthBinding.Source = this;
            var heightBinding = new Binding("ActualHeight");
            heightBinding.Source = this;
            var checkSizeBinding = new Binding("CheckSize");
            checkSizeBinding.Source = this;

            playerLayer.SetBinding(PlayerLayer.HeightProperty, heightBinding);
            playerLayer.SetBinding(PlayerLayer.WidthProperty, widthBinding);
            playerLayer.SetBinding(PlayerLayer.CheckSizeProperty, checkSizeBinding);

            this.Children.Add(playerLayer);
        }

        protected Coordinates GetCheckXY(Point mousePosition)
        {
            var x = (int)(mousePosition.X / CheckSize);
            var y = (int)(mousePosition.Y / CheckSize);
            var xMod = mousePosition.X % CheckSize;
            var yMod = mousePosition.Y % CheckSize;

            if (xMod >= CheckSize / 2)
                x++;

            if (yMod >= CheckSize / 2)
                y++;

            return new Coordinates(x, y);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            return base.MeasureOverride(new Size(_hChecks * CheckSize, _vChecks * CheckSize));
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            return base.ArrangeOverride(new Size(_hChecks * CheckSize, _vChecks * CheckSize));
        }

        protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            // dc.DrawRectangle(Brushes.Transparent, new Pen(Brushes.Green, 2), new Rect(0, 0, this.ActualWidth, this.ActualHeight));

            var lineBrush = new SolidColorBrush(Color.FromArgb(127, 0, 0, 0));
            //draw horizontalLines
            for (int i = 0; i <= _hChecks; i++)
                dc.DrawLine(new Pen(lineBrush, 1), new Point(0, i * CheckSize), new Point(ActualWidth, i * CheckSize));

            //draw verticalLines
            for (int i = 0; i <= _vChecks; i++)
                dc.DrawLine(new Pen(lineBrush, 1), new Point(i * CheckSize, 0), new Point(i * CheckSize, ActualHeight));

            base.OnRender(dc);
        }

        internal Client GameSession
        {
            get { return (Client)DataContext; }
        }

        public int CheckSize
        {
            get { return (int)this.GetValue(CheckSizeProperty); }
            set { this.SetValue(CheckSizeProperty, value); }
        }

        public Size Size
        {
            get { return _size; }
            set { _size = value; }
        }
    }
}
